package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckItemService.class)
@Transactional
public class CheckItemServiceImpl implements CheckItemService {
	@Autowired
	private CheckItemDao checkItemDao;
	@Override
	public void add(CheckItem checkItem) {
		checkItemDao.add(checkItem);
	}

	@Override
	public PageResult pageQuery(QueryPageBean queryPageBean) {
		Integer currentPage = queryPageBean.getCurrentPage();
		Integer pageSize = queryPageBean.getPageSize();
		String queryString = queryPageBean.getQueryString();
		//完成分页查询使用Mybatis框架分页助手
		PageHelper.startPage(currentPage,pageSize);
		Page<CheckItem> page = checkItemDao.selectByCondition(queryString);
		long total = page.getTotal();
		List<CheckItem> rows = page.getResult();
		return new PageResult(total,rows);
	}

	@Override
	public Result deleteById(Integer id) {
		//判断当前检查项是否已经关联检查组
//		long count = checkItemDao.findCountByCheckItemId(id);
//		if(count>0){
//			new RuntimeException();
//		}
		//上面代码改进
		try {
			checkItemDao.deleteById(id);
			return new Result();
		} catch (Exception e) {
			return new Result("该检查项已被检查组引用不能删除");
		}
	}

	@Override
	public CheckItem findById(Integer id) {
		CheckItem checkItem = checkItemDao.findById(id);
		return checkItem;
	}

	@Override
	public void edit(CheckItem checkItem) {
		checkItemDao.edit(checkItem);
	}

	@Override
	public List<CheckItem> findAll() {
		return checkItemDao.findAll();
	}
}
